﻿// CSpellObject.cs
// Author: Gant Briseno
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#endregion

namespace Casters.source.Objects.Spells
{
    /// <summary>
    /// A visible object used for spells
    /// Also, handles damage/status effects given
    /// </summary>
    public class CSpellObject : CRenderObject
    {
        // Variables
        protected SpellMoveType m_MoveType;
        protected CSpell m_Spell;
        protected Vector2 m_vTarget;
        protected float m_fVelocity;
        protected bool m_bPlayAnimation;

        /// <summary>
        /// Sets up the spell object to be rendered
        /// </summary>
        /// <param name="spell">The spell that was cast</param>
        /// <param name="pos">The starting position of the spell</param>
        /// <param name="target">The destination of the spell</param>
        /// <param name="velocity">How fast the spell will move</param>
        public CSpellObject(CSpell spell, Vector2 pos, Vector2 target, float velocity)
        {
            m_vPosition = pos;
            m_Spell = spell;
            m_vTarget = target;
            m_fVelocity = velocity;
            m_MoveType = spell.GetMoveType();

            if (m_MoveType == SpellMoveType.OnTarget)
                m_vPosition = m_vTarget;

            m_bPlayAnimation = false;
            m_bIsAlive = true;
            m_ObjectType = TYPE.Spell;
        }

        /// <summary>
        /// Loads all content for the spell
        /// </summary>
        public override void LoadContent()
        {
            SetupRenderObject(0, null, m_vPosition, Vector2.Zero, Vector2.Zero, "Spells/" + m_Spell.ToString().ToLower());

            if(m_MoveType == SpellMoveType.OnTarget)
                m_AnimPlayer.m_Animation.m_bIsLooping = false;

            base.LoadContent();
        }

        /// <summary>
        /// Unloads all content for the spell
        /// </summary>
        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        /// <summary>
        /// If the spell is a traveling object this will update its
        /// position every turn. Otherwise it will just play the animation;
        /// </summary>
        /// <param name="dt">Delta Time</param>
        public override void Update(float dt)
        {
            if (m_MoveType == SpellMoveType.CasterToTarget)
            {
                Vector2 dir = m_vTarget - m_vPosition;
                m_vPosition += (dir * m_fVelocity) * dt;

                if ((m_vTarget.X <= m_vPosition.X && m_vTarget.X >= m_vPosition.X) &&
                    (m_vTarget.Y <= m_vPosition.Y && m_vTarget.Y >= m_vPosition.Y))
                {
                    CEventSystem.BroadcastEvent(EventID.Execute_SpellHitsTarget);
                    m_bPlayAnimation = true;
                }
            }
            else if (m_MoveType == SpellMoveType.OnTarget)
                m_bPlayAnimation = true;

            base.Update(dt);
        }

        /// <summary>
        /// Draw the spell object
        /// </summary>
        public override void Draw()
        {
            if (m_bPlayAnimation)
            {
                base.Draw();

                if (m_AnimPlayer.m_Animation.m_bIsDonePlaying)
                    m_bIsAlive = false;
            }
        }
    }
}
